#include <windows.h>
#include <stdio.h>
#include <math.h>

HDC hdc;
struct RAMP
{
    WORD Red[256];
    WORD Green[256];
    WORD Blue[256];
} ramp;

double gamma = 1;

LRESULT CALLBACK WProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
      case WM_CREATE:
        hdc = GetDC(NULL);
        RegisterHotKey(hwnd, 1, MOD_ALT, '8');
        RegisterHotKey(hwnd, 2, MOD_ALT, '9');
        RegisterHotKey(hwnd, 3, MOD_ALT, '0');
        RegisterHotKey(hwnd, 4, MOD_ALT | MOD_CONTROL, '8');
        break;

      case WM_HOTKEY:
        switch((int)wParam)
        {
          case 1:
            gamma = 1.0;
            break;
          case 2:
            gamma += .05;
            break;
          case 3:
            gamma -= .05;
            break;
		  case 4:
            DestroyWindow(hwnd);
            return 0;
        }

        if (gamma < 0.3) gamma = 0.3;
        else if (gamma > 3.0) gamma = 3.0;

        for (int i = 0; i < 256; i++)
			ramp.Red[i] = ramp.Green[i] = ramp.Blue[i] = pow(i / 255.0, gamma) * 65535.0;

		SetDeviceGammaRamp(hdc, &ramp);
        break;

      case WM_DESTROY:
        UnregisterHotKey(hwnd, 1);
        UnregisterHotKey(hwnd, 2);
        UnregisterHotKey(hwnd, 3);
        PostQuitMessage(0);
        break;

      default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nCmdShow)
{
    WNDCLASS wc = {0, WProc, 0, 0, hInst, 0, 0, 0, 0, "bright"};
    if (!RegisterClass(&wc) || !CreateWindow("bright","bright",
            0, 0, 0, 0, 0, NULL, NULL, hInst, NULL))
    {
        return 0; //Error
    }

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
